package com.woniu.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Staff;
import com.woniu.entity.SurgeryItem;
import com.woniu.mapper.StaffMapper;
import com.woniu.service.StaffService;
import com.woniu.utils.JWTUtil;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.vo.StaffVO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public ResponseUtil selectAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<StaffVO> staffList = staffMapper.selectVoAll();
        PageInfo pageInfo = new PageInfo(staffList);
        return staffList != null ?ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil updateStaff(Integer id) {
        int rows = staffMapper.updateStaff(id);
        return rows == 1
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil insertSelective(Staff record) {
        if (record.getStatus() == null) {
            record.setStatus(1);   // Integer
        }
        if (record.getIsDeleted() == null) {
            record.setIsDeleted(0); // Integer
        }
        if (record.getPassword() == null || record.getPassword().trim().isEmpty()){
            return ResponseUtil.get(ResponseEnum.FAIL,"密码不能为空");
        }

        String salt = generateSalt();
        String newPassword = generatePassword(record.getPassword(),salt);
        record.setSalt(salt);
        record.setPassword(newPassword);

        int rows = staffMapper.insertSelective(record);
        return rows == 1
                ? ResponseUtil.get(ResponseEnum.OK, record)
                : ResponseUtil.get(ResponseEnum.FAIL);

    }

    @Override
    public ResponseUtil updateByPrimaryKeySelective(Staff record) {
        if (record.getPassword() != null && !record.getPassword().trim().isEmpty()) {
            String salt = generateSalt();
            String newPassword = generatePassword(record.getPassword(), salt);
            record.setSalt(salt);
            record.setPassword(newPassword);
        }
        int rows = staffMapper.updateByPrimaryKeySelective(record);
        return rows == 1
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil selectByCondition(String name, String deptName, String roomName,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<StaffVO> staffList = staffMapper.selectVoByCondition(name,deptName,roomName);
        PageInfo pageInfo = new PageInfo(staffList);
        return staffList!=null ? ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil login(String staffNo, String password) throws Exception {
        if (staffNo.isEmpty() || password.isEmpty()){
            return ResponseUtil.get(ResponseEnum.USER_LOGIN_ERROR_1);
        }
        Staff loginStaff = staffMapper.findByNo(staffNo);
        if (loginStaff == null || loginStaff.getIsDeleted().equals(1)){
            return ResponseUtil.get(ResponseEnum.USER_LOGIN_ERROR_2);
        }else if (!SaSecureUtil.md5(password+loginStaff.getSalt()).equals(loginStaff.getPassword())){
            return ResponseUtil.get(ResponseEnum.USER_LOGIN_ERROR_3);
        }else if(loginStaff.getStatus() !=1){
            return ResponseUtil.get(ResponseEnum.USER_LOGIN_ERROR_4);
        }else {
            loginStaff.setPassword(null);
            loginStaff.setSalt(null);
            HashMap<String, Object> map = new HashMap<>();
            map.put("staff",loginStaff);
            map.put("id",loginStaff.getId());
            String token = JWTUtil.getToken(map);
            stringRedisTemplate.opsForValue().set("jwt37"+loginStaff.getId(),token,120, TimeUnit.MINUTES);
            return ResponseUtil.get(ResponseEnum.USER_LOGIN_SUCCESS,token);

        }

    }

    @Override
    public ResponseUtil findAll() {
        List<Staff> staffList = staffMapper.findAll();
        return staffList != null ?ResponseUtil.get(ResponseEnum.OK,staffList)
                :ResponseUtil.get(ResponseEnum.FAIL);
    }


    private String generateSalt() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 加密密码
     */
    private String generatePassword(String password, String salt) {
        String s = SaSecureUtil.md5(password);
        return SaSecureUtil.md5(s + salt);
    }
}
